// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import 'babel-polyfill';
import Vue from 'vue'
// 路由
import VueRouter from 'vue-router'
// 同步路由
import { sync } from 'vuex-router-sync'
// 标题
import VueMeta from 'vue-meta'
// vux ui 工具库 集成 微信等工具包
import { ToastPlugin, LoadingPlugin, WechatPlugin, AlertPlugin,DatetimePlugin,ConfirmPlugin } from 'vux'
// 路由承载
import App from './App'
import router from './router'
import store from 'store/store'
import httpPlugin from 'utils/http'
// 获取用户缓存
import {getStore} from '@/config/mUtils'
// 重定向url 地址
import {url,loginUrl,loginUrl1,appId,nativeto} from '@/config/config'
// 转换rem 单位
import './config/rem.js'
// 样式
import './styles/app.css'
Vue.use(VueMeta)
Vue.use(ToastPlugin)    // 集成使用提醒
Vue.use(LoadingPlugin)  // 集成使用Loading插件
Vue.use(WechatPlugin)   // 集成微信js-sdk
Vue.use(AlertPlugin)    // 集成alert的使用
Vue.use(DatetimePlugin) //时间插件
Vue.use(ConfirmPlugin) //确认提示框
Vue.use(VueRouter)
Vue.use(httpPlugin, { store, router })
Vue.config.productionTip = false
const getUrlParam = (name)=>{
  var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
  var r = window.location.search.substr(1).match(reg);  //匹配目标参数
  if (r!=null) return unescape(r[2]); return null; //返回参数值
}
// 路由拦截 切换加载效果,使用vuex来实现
router.beforeEach(function (to, from, next) {
  store.commit('UPDATE_LOADING_STATUS', {isLoading: true})
  // 首页重定向授权
  let domain_url_find=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${encodeURIComponent(url)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`;  
  // 登入页面重定向授权
  let login_url_find = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${encodeURIComponent(loginUrl)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
  // 1
  let login_url_find1 = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${encodeURIComponent(loginUrl1)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
  // console.log(getStore('name'));
  if(!getStore('wxcode')){
    // 首页不需用户 code
    if(to.path === '/'){
        var Code=getUrlParam('code');
        if(Code){
          store.dispatch('setWxCode',Code)
          next()
        }else{
          window.location.href=domain_url_find
          next()
        }
    }else if(to.path === '/login'){
      //这个登入code 是临时的。app 做好之后可能会取消; 
      next()
      // var Code=getUrlParam('code');
      // if(Code){
      //   store.dispatch('setWxCode',Code)
      //   next()
      // }else{
      //   window.location.href= login_url_find
      //   next()
      // }
    }else if(to.path === '/shop/order'){
      next()
    }else if(to.path === '/nativeto'){
      if(to.query.val){
        store.dispatch('setQcode',to.query.val);
        var Code=getUrlParam('code');
        if(Code){
          store.dispatch('setWxCode',Code)
          next()
        }else{
           // 外面扫码购买商品临时授权
          let nativeto_url_find = nativeto+'?val='+to.query.val;
          window.location.href= `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${encodeURIComponent(nativeto_url_find)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
          next()
        }
      }else{
        alert('二维码错误')
      }
    }else if(to.path ==='/bindOpenId'){
      next()
      // var Code=getUrlParam('code');
      // if(Code){
      //   store.dispatch('setWxCode',Code)
      //   next()
      // }else{
      //   window.location.href = login_url_find1
      //   next()
      // }
    }else{
      alert('没有登入或登入信息过期,即将跳转到首页自动登入');
          setTimeout(()=>{
              window.location.href=domain_url_find;
          },1000)
    }
  }else{
    if(to.query.val){
      store.dispatch('setQcode',to.query.val);
    }
    next()
  }
  // next();
})
router.afterEach(function (to) {
  store.commit('UPDATE_LOADING_STATUS', {isLoading: false})
})
sync(store, router)
/* eslint-disable no-new */
new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app')
